/**
  *@File文件:UserService.java
  *@Date日期:2017年7月14日 上午9:34:33
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.serviceImp;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qiaomengapp.bean.User;
import com.qiaomengapp.dao.UserMapper;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IUserService;

/**
 *@Description描述: 用户
 *@Author作者 jinxin
 *@Date日期:2017年7月14日
 */
@Service
public class ImpUserService implements IUserService{
	@Autowired
	private UserMapper userMapper;
	//用户登录
	public User select_login(String phone,String password) throws CustomException{
		User user = userMapper.select_login(phone, password);
		if(user==null){
			throw new CustomException("用户名或密码错误");
		}
		return user;
	}
	//注册账户时,验证手机号是否被注册
	public User select_validatePhone(String phone)throws CustomException{
		User user = userMapper.select_validatePhone(phone);
		if(user!=null){
			throw new CustomException("手机号已被注册");
		}
		return user;
	}
	//找回密码时,验证手机号是否被注册
	public User select_validatePhone2(String phone)throws CustomException{
		User user = userMapper.select_validatePhone(phone);
		if(user==null){
			throw new CustomException("输入手机号错误");
		}
		return user;
	}
	//用户注册
	public int insert_register(String phone,String password) throws CustomException{
		//验证手机号是否被注册过,找回密码的手机号验证
		int i = userMapper.insert_register(phone, password);
		if(i==0){
			throw new CustomException("注册账号失败");
		}
		return i;
	}
	//设置新密码
	public int update_password(int userId,String newPassword) throws CustomException{
		User user = userMapper.select_findUser(userId);
		if(user.getPassword().equals(newPassword)){
			throw new CustomException("旧密码与新密码一致,请重试");
		}
		int i = userMapper.update_password(userId, newPassword);
		if(i==0){
			throw new CustomException("修改密码失败");
		}
		return i;
	}
	//忘记密码的设置新密码
	public int update_password2(String phone,String newPassword) throws CustomException{
		int i = userMapper.update_password2(phone, newPassword);
		if(i==0){
			throw new CustomException("修改密码失败");
		}
		return i;
	}
	
	//添加,修改用户昵称
	public	int update_name(int userId,String name) throws CustomException{
		int i = userMapper.update_name(userId, name);
		if(i==0){
			throw new CustomException("修改用户昵称失败");
		}
		return i;
		
	}
	//添加,修改用户头像
	public	int update_headImg(int userId,String headImg) throws CustomException{
		int i = userMapper.update_headImg(userId, headImg);
		if(i==0){
			throw new CustomException("修改用户头像失败");
		}
		return i;
		
	}
	//添加,修改用户个人介绍
	public	int update_userDesc(int userId,String userDesc) throws CustomException{
		int i = userMapper.update_userDesc(userId, userDesc);
		if(i==0){
			throw new CustomException("修改用户个人介绍失败");
		}
		return i;
		
	}
                                                                                                                      
	//添加,修改默认地址
	public int update_defaultAddressId(int userId,int userAddressId) throws CustomException{
		int i = userMapper.update_defaultAddressId(userId, userAddressId);	
		if(i==0){
			throw new CustomException("修改用户默认地址失败");
		}
		return i;
	}
}

